package com.ygo.feihua.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import cn.bmob.v3.datatype.BmobFile;
import com.bumptech.glide.Glide;
import com.ygo.feihua.BmobTable.GuangGao;
import com.ygo.feihua.BmobTable.ZhuJian;
import com.ygo.feihua.Gongju;
import com.ygo.feihua.R;
import java.util.List;
import android.content.Intent;
import com.ygo.feihua.ImageXiangxi;
import android.widget.*;
import com.ygo.feihua.view.*;
import com.ygo.feihua.*;
import com.ygo.feihua.util.*;

/**
 * 广告轮播adapter
 *@author dong
 *@data 2015年3月8日下午3:46:35
 *@contance dong854163@163.com
 */
public class AdvertisementAdapter extends PagerAdapter {

	private Context context;
	private List<View> views;
	int gg;
	List<Object> data;
	Gongju gj;
	private DialogUtils du;
	
	public AdvertisementAdapter() {
		super();
		// TODO Auto-generated constructor stub
	}

	public AdvertisementAdapter(Context context, List<View> views, List<Object> data,int gg) {
		this.context = context;
		this.views = views;
		this.data=data;
		this.gg=gg;
		gj=Gongju.getdx(context);
		du=DialogUtils.getdx(context);
	}

	public List<Object> getdata(){
		
		return data;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return views.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return (arg0 == arg1);
	}
	
	@Override
	public void destroyItem(View container, int position, Object object) {
		((ViewPager) container).removeView(views.get(position));
		
		
	}

	@Override
	public Object instantiateItem(final View container,final int position) {
		((ViewPager) container).addView(views.get(position));
		
		View view = views.get(position);
		try {
			ImageView ivAdvertise = (ImageView) view.findViewById(R.id.ivAdvertise);
			switch(gg){
				case 0:
					final GuangGao guangao=(GuangGao) data.get(position);
				/*	Uri zttj1 =Uri.parse( guangao.getGg_tp().getFileUrl());
					Glide.with(context).load(zttj1).placeholder(R.drawable.image_jiazai).into(ivAdvertise);
					*/
					gj.tuxian(guangao.getGg_tp(),ivAdvertise);
					ivAdvertise.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								switch(guangao.getGg_lx()){
									case 0:
										//跳转网址
										context.startActivity(IntentUtil.getUrlIntent(context,guangao.getGg_wz()));
										break;
									case 1:
										//跳转帖子
										gj.tieziIntent(guangao.getGg_wz());
										break;
								}
								// TODO: Implement this method
							}
						});
				
					break;
				case 1:
					final ZhuJian zj=(ZhuJian) data.get(position);
					gj.tuxian(zj.getZt_tjt().getFileUrl(),ivAdvertise);
					ivAdvertise.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								//跳转主题
								IntentUtil.zhutiIntent(context,zj.getId());
								// TODO: Implement this method
							}
						});
					
					break;
				case 2:
					final BmobFile bff=(BmobFile) data.get(position);
					/*Uri ztbf =Uri.parse( bff.getFileUrl());
					Glide.with(context).load(ztbf).placeholder(R.drawable.image_jiazai).into(ivAdvertise);*/
					gj.tuxian(bff,ivAdvertise);
					ivAdvertise.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								Intent intent1 =new Intent(context,ImageXiangxi.class);
								intent1.putExtra("uri",bff.getFileUrl());
								context.startActivity(intent1);
								// TODO: Implement this method
							}
						});
					break;
				case 3:
					final String path=(String) data.get(position);
					
					Glide.with(context).load(path).into(ivAdvertise);
					
					ivAdvertise.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								final ZhutiShangchuan zs=(ZhutiShangchuan) context;
								if(zs.zt_jt.size()==1){
									gj.show("至少有一张预览图");
								}else{
								View v[] =du.dialogt(false,"","是否删除该图片");
								
								Button b1,b2;
								b1=(Button) v[0];
								b2=(Button) v[1];
								b1.setText("取消");
								b2.setText("删除");
								b1.setOnClickListener(new OnClickListener(){

										@Override
										public void onClick(View p1)
										{
											du.dis();
											// TODO: Implement this method
										}
									});	
									
								b2.setOnClickListener(new OnClickListener(){

										@Override
										public void onClick(View p1)
										{
											
										//	views.remove(position);
											du.dis();
										
											
										
										zs.zt_jt.remove(position);
										zs.fagzs.reGuanggao(new Advertisements(context, true, 3000, 3)
												.initView((List<Object>)zs.zt_jt));
									
											/*data.remove(position);
											views.remove(position);
											notifyDataSetChanged();
											Log.e("data剩余",views.size()+"剩余数量"+data.size());
											*/
										
											// TODO: Implement this method
										}
									});
								}
								
								// TODO: Implement this method
							}
						});
						
					break;
					
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
}
